package cn.usts.edu.Reflection03;

/**
 * @author ：fly
 * @description: 获取class对象的方法
 * @date ：2021/10/27 14:11
 */
public class GetClassDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Teacher();
        System.out.println("这个人是"+person.name);

        // 方式1: 通过对象获取
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        // 方式2: forName获取
        Class c2 = Class.forName("cn.usts.edu.Reflection03.Teacher");
        System.out.println(c2.hashCode());

        // 方式3:通过类名.class获取
        Class c3 = Teacher.class;
        System.out.println(c3.hashCode());

        // 方式4: 内置类型的包装类都有一个Type属性
        Class c4 = Integer.TYPE;
        System.out.println(c4);

        // 方式5: 通过父类获取
        Class c5 = c1.getSuperclass();
        System.out.println(c5);
    }
}

// pojo1
class Person{
    int age;
    String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Person() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

// pojo2 教师
class Teacher extends Person{
    public Teacher() {
        this.age = 30;
        this.name = "老师";
    }
}

// pojo3 学生
class Student extends Person{
    public Student() {
        this.age=18;
        this.name="学生";
    }
}